package com.jrx.anytxn.param.controller.account;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.account.AccountingCheckConfigReq;
import com.jrx.anytxn.param.dto.account.AccountingCheckConfigRes;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigReq;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.account.IAccountingCheckConfigService;
import com.jrx.anytxn.param.service.account.ISubjectAccountConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 与页面相关的总分核对配置controller
 *
 * @author xiaoyu.jing
 * date 2019/12/24 0024
 * @version V1.0
 **/
@Api(tags = "总分核对配置api")
@RestController
@RequestMapping("/param")
public class AccountingCheckConfigController {

    private static final Logger logger = LoggerFactory.getLogger(AccountingCheckConfigController.class);

    @Autowired
    private IAccountingCheckConfigService accountingCheckConfigService;

    @Autowired
    private InfoService infoService;

    @ApiOperation("根据租户ID分页查询总分核对配置信息")
    @GetMapping("/accountingCheckConfig/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<AccountingCheckConfigRes>> getAllAccountingCheckConfigByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<AccountingCheckConfigRes>> result = new TxnRespResult<>();
        TxnPage<AccountingCheckConfigRes> res;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            res = accountingCheckConfigService.getAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("通过科目表ID和租户Id查询信息")
    @GetMapping("/accountingCheckConfig/tenantId/{tenantId}/productId/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "productId", value = "产品ID", required = true)})
    public TxnRespResult<AccountingCheckConfigRes> getBySubjectIdAndOrgId(@PathVariable("tenantId") String tenantId,@PathVariable("productId") String productId) {
        TxnRespResult<AccountingCheckConfigRes> result = new TxnRespResult<>();
        AccountingCheckConfigRes res;
        try {
            res = accountingCheckConfigService.getByTenIdIdAndProductId(String.valueOf(infoService.getUser().getProjectId()),productId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("增加科目总分核对配置信息")
    @PostMapping("/accountingCheckConfig")
    public TxnRespResult<AccountingCheckConfigRes> createSubjectAccountConfig(@RequestBody AccountingCheckConfigReq accountingCheckConfigReq) {
        TxnRespResult<AccountingCheckConfigRes> result = new TxnRespResult<>();
        AccountingCheckConfigRes res;
        try {
            accountingCheckConfigReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            accountingCheckConfigReq.setUpdateBy(infoService.getUser().getUserName());
            res = accountingCheckConfigService.addAccountingCheckConfig(accountingCheckConfigReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("修改科目总分核对配置信息")
    @PutMapping("/accountingCheckConfig")
    public TxnRespResult<AccountingCheckConfigRes> modifySubjectAccountConfig(@RequestBody AccountingCheckConfigReq accountingCheckConfigReq) {
        TxnRespResult<AccountingCheckConfigRes> result = new TxnRespResult<>();
        AccountingCheckConfigRes res;
        accountingCheckConfigReq.setUpdateBy(infoService.getUser().getUserName());
        res = accountingCheckConfigService.modifyAccountingCheckConfig(accountingCheckConfigReq);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation("根据租户ID和科目表ID删除总分核对配置信息")
    @DeleteMapping("/accountingCheckConfig/{tenantId}/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "productId", value = "产品ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndProIdAndSubId(@PathVariable(value = "tenantId") String tenantId,@PathVariable("productId") String productId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = accountingCheckConfigService.removeByProIdAndTenId(String.valueOf(infoService.getUser().getProjectId()), productId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


}
